package com.test.p01singleton.example06;

import com.test.p01singleton.example01.Singleton01;
import com.test.p01singleton.example02.Singleton02;
import com.test.p01singleton.example03.Singleton03;
import com.test.p01singleton.example04.Singleton04;
import com.test.p01singleton.example05.Singleton05;

import java.util.concurrent.CountDownLatch;

/**
 * 比较多线程下
 * 各种单例模式的效率
 */
public class Client02 {

    /**
     * 饿汉：85
     * 懒汉：1734
     * 双重检测：94
     * 静态内部类 94
     * 枚举：82
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {

        long start = System.currentTimeMillis();
        int threadNum =1000;
        final CountDownLatch count = new CountDownLatch(threadNum);
        for(int i=0;i<threadNum;i++) {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    for (int i = 0; i < 100000; i++) {
//                        Object o1 = Singleton01.getInstance();
//                        Object o2 = Singleton02.getInstance();
//                        Object o3 = Singleton03.getInstance();
//                        Object o4 = Singleton04.getInstance();
                        Object o5 = Singleton05.INSTANCE;
                    }
                    count.countDown();
                }
            }).start();
        }
        count.await();
        System.out.println("总耗时："+(System.currentTimeMillis()-start));
    }
}
